from pymongo import MongoClient

class mgadapter:
    data=[]
    def __init__(self):
        pass
    def add_str(self,s):
        self.data.append(s)

    def sub_mit(self):
        if len(self.data)==0:
            return True

        try:
            conn = MongoClient('127.0.0.1', 27017)
            db=conn.studyurl
            dbset=db.mp4url

            count=int(dbset.find_one({'url':'numcount'})['num'])
            rdata=[]
            for item in self.data:
                count=count+1
                temp={}
                temp['url']=item
                temp['num']=str(count)
                rdata.append(temp)

            print('reday to insert: '+str(len(rdata)))
            dbset.insert(rdata)
            dbset.update({"url":"numcount"},{'$set':{"num":str(count)}})

            conn.close()
            return True
        except Exception as e:
            print(e)
            return False
        



